﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data.Linq;
using DataLayer.dbml;
using DataLayer.Controller;
using DotNetNuke.Services.Scheduling;
using DotNetNuke.Services.Exceptions;


namespace ScheduledReenvioWS
{
    public class ReenvioWS : SchedulerClient
    {
        public ReenvioWS(ScheduleHistoryItem objScheduleHistoryItem) : base()
        {
            ScheduleHistoryItem = objScheduleHistoryItem;
        }

        public override void DoWork()
        {
            try
            {
                LogTransactionController obj = new LogTransactionController();
                string resultado = obj.Reenviar();
                ScheduleHistoryItem.AddLogNote(resultado);
                ScheduleHistoryItem.Succeeded = true;
            }
            catch (Exception ex)
            {
                ScheduleHistoryItem.Succeeded = false;

                string mensaje = ex.Message;
                if (ex.InnerException != null)
                    mensaje = mensaje + " INNER EXCEPTION: " + ex.InnerException.ToString();

                ScheduleHistoryItem.AddLogNote("Ocurrió un error al ejecutar la tarea: " + mensaje);                

                // call the Errored method
                Errored(ref ex);

                // log the exception into the DNN core
                Exceptions.LogException(ex);
            }
        }

        private void Prueba()
        {
            // perform some task                
            String strPath = @"E:\CS_DID_IT.TXT";
            string cedula = ConfigurationSettings.AppSettings["PruebaSchedule"].ToString();
            using (StreamWriter sw = new StreamWriter(strPath, true))
            {
                sw.WriteLine(DateTime.Now.ToString() + " - C# DID IT: " + cedula);
                sw.Close();
            }
        }

    }
}
